home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlfind.zip / TREE.C < prev    next >
C/C++ Source or Header  |  1990-07-02  |  5KB  |  154 lines

  1. /* Routines to build and evaluate the expression tree.
  2.    Copyright (C) 1987, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>
  20. #include "defs.h"
  21.  
  22. struct pred_struct *scan_rest ();
  23.  
  24.  
  25. /* Return a pointer to a tree that represents the
  26.    expression prior to non-unary operator *INPUT.
  27.    Set *INPUT to point at the next input predicate node.
  28.  
  29.    Only accepts the following:
  30.    
  31.    <victim>
  32.    expression        [operators of higher precedence]
  33.    <uni_op><victim>
  34.    (arbitrary expression)
  35.    <uni_op>(arbitrary expression)
  36.    
  37.    In other words, you can not start out with a bi_op or close_paren.
  38.  
  39.    If the following operator (if any) is of a higher precedence than
  40.    PREV_PREC, the expression just nabbed is part of a following
  41.    expression, which really is the expression that should be handed to
  42.    our caller, so get_expr recurses. */
  43.  
  44. struct pred_struct *
  45. get_expr (input, prev_prec)
  46.      struct pred_struct **input;
  47.      short prev_prec;
  48. {
  49.   struct pred_struct *next;
  50.  
  51.   if (*input == NULL)
  52.     error (1, 0, "invalid expression");
  53.   switch ((*input)->p_type)
  54.     {
  55.     case NO_TYPE:
  56.     case BI_OP:
  57.     case CLOSE_PAREN:
  58.       error (1, 0, "invalid expression");
  59.       break;
  60.  
  61.     case VICTIM_TYPE:
  62.       next = *input;
  63.       *input = (*input)->pred_next;
  64.       break;
  65.  
  66.     case UNI_OP:
  67.       next = *input;
  68.       *input = (*input)->pred_next;
  69.       next->pred_left = get_expr (input, NEGATE_PREC);
  70.       break;
  71.  
  72.     case OPEN_PAREN:
  73.       *input = (*input)->pred_next;
  74.       next = get_expr (input, NO_PREC);
  75.       if ((*input == NULL)
  76.       || ((*input)->p_type != CLOSE_PAREN))
  77.     error (1, 0, "invalid expression");
  78.       *input = (*input)->pred_next;    /* move over close */
  79.       break;
  80.  
  81.     default:
  82.       error (1, 0, "oops -- invalid expression type!");
  83.       break;
  84.     }
  85.  
  86.   /* We now have the first expression and are positioned to check
  87.      out the next operator.  If NULL, all done.  Otherwise, if
  88.      PREV_PREC < the current node precedence, we must continue;
  89.      the expression we just nabbed is more tightly bound to the
  90.      following expression than to the previous one. */
  91.   if (*input == NULL)
  92.     return (next);
  93.   if ((int) (*input)->p_prec > (int) prev_prec)
  94.     {
  95.       next = scan_rest (input, next, prev_prec);
  96.       if (next == NULL)
  97.     error (1, 0, "invalid expression");
  98.     }
  99.   return (next);
  100. }
  101.  
  102. /* Scan across the remainder of a predicate input list starting
  103.    at *INPUT, building the rest of the expression tree to return.
  104.    Stop at the first close parenthesis or the end of the input list.
  105.    Assumes that get_expr has been called to nab the first element
  106.    of the expression tree.
  107.    
  108.    *INPUT points to the current input predicate list element.
  109.    It is updated as we move along the list to point to the
  110.    terminating input element.
  111.    HEAD points to the predicate element that was obtained
  112.    by the call to get_expr.
  113.    PREV_PREC is the precedence of the previous predicate element. */
  114.  
  115. struct pred_struct *
  116. scan_rest (input, head, prev_prec)
  117.      struct pred_struct **input;
  118.      struct pred_struct *head;
  119.      short prev_prec;
  120. {
  121.   struct pred_struct *tree;    /* The new tree we are building. */
  122.  
  123.   if ((*input == NULL) || ((*input)->p_type == CLOSE_PAREN))
  124.     return (NULL);
  125.   tree = head;
  126.   while ((*input != NULL) && ((int) (*input)->p_prec > (int) prev_prec))
  127.     {
  128.       switch ((*input)->p_type)
  129.     {
  130.     case NO_TYPE:
  131.     case VICTIM_TYPE:
  132.     case UNI_OP:
  133.     case OPEN_PAREN:
  134.       error (1, 0, "invalid expression");
  135.       break;
  136.  
  137.     case BI_OP:
  138.       (*input)->pred_left = tree;
  139.       tree = *input;
  140.       *input = (*input)->pred_next;
  141.       tree->pred_right = get_expr (input, tree->p_prec);
  142.       break;
  143.  
  144.     case CLOSE_PAREN:
  145.       return (tree);
  146.  
  147.     default:
  148.       error (1, 0, "oops -- invalid expression type!");
  149.       break;
  150.     }
  151.     }
  152.   return (tree);
  153. }
  154.